package View;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ue.UE;
import eNodeB.ENodeB;
import eNodeB.Fabrique;

public class MapPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8934029832649521694L;

	private final ImageIcon _tile;

	public MapPanel(ImageIcon tile) {
		_tile = tile;
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);

		setLayout(null);

		ENodeB e = Fabrique.getFabrique().getEnodeB();
		if (e != null)
			e.getImage().paintIcon(this, g, (int) e.getPositionX(),
					(int) e.getPositionY());
		try {
			for (UE ue : Fabrique.getFabrique().getEnodeB().getActualUE()) {
				ue.getImage().paintIcon(this, g, (int) ue.getPositionX(),
						(int) ue.getPositionY());
			}
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(this, "Pas de Users !");
		}
	}

	@Override
	public void paintComponent(Graphics g) {
		int tWidth = _tile.getIconWidth();
		int tHeight = _tile.getIconHeight();
		int posX = 0;
		int posY = 0;

		Graphics2D g2d = (Graphics2D) g;

		for (int i = 0; i < (this.getHeight() / tHeight) + 1; i++) {
			for (int j = 0; j < (this.getWidth() / tWidth) + 1; j++) {
				g2d.drawImage(_tile.getImage(), posX, posY, this);
				posX += tWidth;
			}
			posX = 0;
			posY += tHeight;
		}
	}
}
